package dao.impl;

import dao.BaseDao;
import dao.OrderDao;
import pojo.Order;

import java.util.List;

/**
 * @author Wxt
 * @create 2022-02-11 20:54
 */
public class OrderDaoImpl extends BaseDao implements OrderDao {
    @Override
    public int saveOrder(Order order) {
        String sql = "insert into t_order(order_id,create_time,price,status,user_id) values(?,?,?,?,?)";
        return update(sql, order.getOrderId(), order.getCreateTime(), order.getPrice(), order.getStatus(), order.getUserId());
    }

    @Override
    public List<Order> queryOrders() {
        String sql = "select `order_id` orderId,`create_time` createTime,price,status,`user_id` userId from t_order";
        return queryForList(Order.class, sql);
    }

    @Override
    public int changeOrderStatus(String orderId, int status) {
        String sql = "update t_order set status = ? where order_id = ?";
        return update(sql, status, orderId);
    }

    @Override
    public int queryForPageTotalCount() {
        String sql = "select count(*) from t_order";
        Number number = querySingleValue(sql);
        return number.intValue();
    }

    @Override
    public List<Order> queryOrdersByUserId(int userId) {
        String sql = "select `order_id` orderId,`create_time` createTime,price,status,`user_id` userId from t_order where user_id = ?";
        return queryForList(Order.class, sql, userId);
    }

    @Override
    public List<Order> queryForOrders(int begin, int pageSize) {
        String sql = "select `order_id` as orderId,`create_time` as createTime,price,status,`user_id` as userId " +
                "from t_order limit ?,?";
        return queryForList(Order.class, sql, begin,pageSize);
    }
}
